# backend/utils/response.py
# utils/response.py
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder   # ★ 新增

class R:
    @staticmethod
    def ok(data=None, message="success"):
        payload = {"code": 0, "message": message, "data": data}
        # jsonable_encoder 会把 Pydantic/BaseModel 自动转成可 JSON 序列化的 dict
        return JSONResponse(content=jsonable_encoder(payload))

    @staticmethod
    def error(code=1, message="error", data=None):
        return JSONResponse(
            content=jsonable_encoder({"code": code, "message": message, "data": data})
        )